package thread.knn.task;

import thread.knn.bean.BankMarketing;
import thread.knn.bean.Distance;
import thread.knn.bean.Sample;
import thread.knn.distanceAlgorithm.DistanceCalculator;
import thread.knn.distanceAlgorithm.EuclideanDistanceCalculator;

import java.util.List;

public class GroupDistanceTask implements Runnable{
    private Distance[] distances;
    private int startIndex;
    private int endIndex;
    private Sample sample;
    private List<BankMarketing> data;//样本集
    private DistanceCalculator distanceCalculator;

    public DistanceCalculator getDistanceCalculator() {
        return distanceCalculator;
    }

    public void setDistanceCalculator(DistanceCalculator distanceCalculator) {
        this.distanceCalculator = distanceCalculator;
    }

    public GroupDistanceTask(Distance[] distances, int startIndex, int endIndex, Sample sample, List<BankMarketing> data) {
        this.distances = distances;
        this.startIndex = startIndex;
        this.endIndex = endIndex;
        this.sample = sample;
        this.data = data;
    }

    public GroupDistanceTask(Distance[] distances, int startIndex, int endIndex, Sample sample, List<BankMarketing> data, DistanceCalculator distanceCalculator) {
        this.distances = distances;
        this.startIndex = startIndex;
        this.endIndex = endIndex;
        this.sample = sample;
        this.data = data;
        this.distanceCalculator = distanceCalculator;
    }



    public Distance[] getDistances() {
        return distances;
    }

    public void setDistances(Distance[] distances) {
        this.distances = distances;
    }

    public int getStartIndex() {
        return startIndex;
    }

    public void setStartIndex(int startIndex) {
        this.startIndex = startIndex;
    }

    public int getEndIndex() {
        return endIndex;
    }

    public void setEndIndex(int endIndex) {
        this.endIndex = endIndex;
    }

    public Sample getSample() {
        return sample;
    }

    public void setSample(Sample sample) {
        this.sample = sample;
    }

    public List<BankMarketing> getData() {
        return data;
    }

    public void setData(List<BankMarketing> data) {
        this.data = data;
    }

    /**
     * 从startIndex到endIndex计算sample和data中每个样本的距离
     */
    @Override
    public void run() {
        distanceCalculator = new EuclideanDistanceCalculator();
        for (int i=startIndex;i<endIndex;i++){
            Sample s = data.get(i);
//            for(int j=0;j<data.size();j++){

                //计算s和sample的距离
                distances[i] = new Distance();
                distances[i].setIndex(i);
                distances[i].setDistance(this.distanceCalculator.calculate(s,sample));

                //System.out.println("当前线程："+Thread.currentThread().getName()+" 计算第"+i+"个样本数据完成");
//            }

        }
        System.out.println("当前线程完成："+Thread.currentThread().getName());
    }
}
